#define _CRT_SECURE_NO_WARNINCS
#include<stdio.h>
#include<stdlib.h>
#include <assert.h>
#include<math.h>

struct ListNode {
    int val;
    struct ListNode* next;
};

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode* newlist = (struct ListNode*)malloc(sizeof(struct ListNode));
    newlist->next = NULL;
    if (l1->val == 0)
    {
        return l2;
    }
    else if (l2->val == 0)
    {
        return l1;
    }
    else if (l1->val && l2->val == 0)
    {
        newlist->val = 0;
        return newlist;
    }
    int sum = 0;
    struct ListNode* cur1 = l1, * cur2 = l2;
    int t = 0;
    while (cur1)
    {

        sum += (cur1->val) * pow(10, t);
        t++;
        cur1 = cur1->next;
    }
    t = 0;
    while (cur2)
    {

        sum += (cur2->val) * pow(10, t);
        t++;
        cur2 = cur2->next;
    }

    struct ListNode* newcur = newlist;
    newcur->val = sum % 10;
    newcur->next = NULL;
    sum /= 10;
    while (sum)
    {
        struct ListNode* nextnode = (struct ListNode*)malloc(sizeof(struct ListNode));
        nextnode->val = sum % 10;
        newcur->next = nextnode;
        nextnode->next = NULL;
        newcur = nextnode;
        sum /= 10;
    }
    return newlist;
}


int main()
{
    struct ListNode* l1;
    l1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    l1->next = NULL;
    l1->val = 0;

    struct ListNode* cur1 = l1;
    cur1->val = 9;

   /* struct ListNode* next1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    next1->val = 4;

    struct ListNode* next2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    next2->val = 3;

    cur1->next = next1;
    next1->next = next2;
    next2->next = NULL;*/
 

    struct ListNode* l2;
    l2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    l2->next = NULL;
    l2->val = 1;

    struct ListNode* cur2 = l2;
    cur2->val = 1;

    struct ListNode* next3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    next3->val = 9;
    
    struct ListNode* next4 = (struct ListNode*)malloc(sizeof(struct ListNode));
    next4->val = 9;
    cur2->next = next3;
    next3->next = next4;
    next4->next = NULL;

    struct ListNode*newhead = addTwoNumbers(l1, l2);

    assert(newhead);
    while (newhead)
    {
        printf("%d->", newhead->val);
        newhead = newhead->next;
    }
	return 0;
}